module(..., package.seeall) --使得文件中的函数在何处都可调用
require "misc"
require "net"
require "sim"
require "rtos"
require "math"
require "nvm"
require "logGenerator"
require "mqttOutMsg"
--[[
功能  ：获取设备参数
参数  ：无
返回值：无
]]
local slen, logInfo, errInfo, sysTime = string.len, "", "", ""
--获取运营商
local function getMobileCardOperator()
    local operatoName, operatorNo = "", tostring(sim.getMcc()) .. tostring(sim.getMnc())
    if "46000" == operatorNo or "46002" == operatorNo or "46004" == operatorNo or "46007" == operatorNo then
        operatoName = "中国移动"
    elseif "46001" == operatorNo or "46006" == operatorNo or "46009" == operatorNo then
        operatoName = "中国联通"
    elseif "46003" == operatorNo or "46005" == operatorNo or "46011" == operatorNo then
        operatoName = "中国电信"
    else
        operatoName = "其他"
    end
    return operatoName
end

--获取可用空间
local function getAvailableSpace()
    local availableSpace, usedSpace, totalSpace = 0.0, rtos.get_fs_free_size(), rtos.get_fs_total_size()
    availableSpace = math.floor(usedSpace / totalSpace * 100)
    return availableSpace
end

--获取信号类型
local function getSignalType()
    local signalType, singalTypeNo = "", net.getNetMode()
    if singalTypeNo == 0 then
        signalType = "未注册"
    elseif singalTypeNo == 1 then
        signalType = "2G"
    elseif singalTypeNo == 2 then
        signalType = "2.5G"
    elseif singalTypeNo == 3 then
        signalType = "3G"
    elseif singalTypeNo == 4 then
        signalType = "4G"
    elseif singalTypeNo == 5 then
        signalType = "5G"
    end
    return signalType
end

function getAndSaveDevStatus()
    --设备号
    local clientNo = nvm.get("info")[1]
    --主板
    local mainBoard = "Air724UG"
    --固件版本
    local firmwareVersion = misc.getVersion()
    --有线MAC
    local wiredMAC = ""
    --无线MAC
    local wirelessMAC = ""
    --IMEI
    local IMEI = misc.getImei()
    --终端当前时间
    local terminalCurrentTime = string.format("%04d-%02d-%02d %02d:%02d:%02d", os.date("*t").year,
        os.date("*t").month, os.date("*t").day, os.date("*t").hour, os.date("*t").min, os.date("*t").sec)
    --4G信号强度
    local signalStrength4G = net.getRssi()
    --信号类型
    local signalType = getSignalType()
    --运营商
    local operator = getMobileCardOperator()
    --卡号
    local cardNo = sim.getIccid()
    --蓝牙状态
    local bluetoothStatus = false
    --热点状态
    local hotspotStatus = false
    --WiFi状态
    local wiFiStatus = false
    --可用空间
    local availableSpace = getAvailableSpace()
    --电量
    local electricQuantity = 100
    --DPIS
    local DPIS = 160
    --TTS状态
    local TTS_status = true
    --VPN开启状态
    local VPN_on_state = false
    --守护状态
    local guardState = true
    log.info("time", terminalCurrentTime)
    local topic = "action"
    local sendMsg = "{" .. "\"todo\":\"devStatus\",\"wifi\":" ..
        tostring(wiFiStatus) .. ",\"signalStrength\":" .. signalStrength4G ..
        ",\"timeNow\":\"" .. terminalCurrentTime .. "\",\"guard\":" .. tostring(guardState) ..
        ",\"clientNo\":\"" .. clientNo .. "\",\"IMEI\":\"" .. IMEI .. "\",\"etherMac\":\"" .. wiredMAC ..
        "\",\"wifiAp\":" .. tostring(hotspotStatus) .. ",\"battery\":" .. electricQuantity ..
        ",\"space\":" .. availableSpace .. ",\"iccid\":\"" .. cardNo ..
        "\",\"tts\":" .. tostring(TTS_status) .. ",\"bluetooth\":" .. tostring(bluetoothStatus) ..
        ",\"operators\":\"" .. operator .. "\",\"vpn\":" .. tostring(VPN_on_state) ..
        ",\"signalType\":\"" .. signalType .. "\",\"dpis\":" .. DPIS ..
        ",\"wifiMac\":\"" .. wirelessMAC .. "\",\"firmware\":\"" .. firmwareVersion ..
        "\",\"board\":\"" .. mainBoard .. "\"}"
    log.info("json", sendMsg)
    print("availableSpace: " .. availableSpace .. " Bytes")
    sendMsg = crypto.aes_encrypt("ECB", "PKCS5", sendMsg, "keson-123abcdefg")
    sendMsg = crypto.base64_encode(sendMsg, slen(sendMsg))
    mqttOutMsg.insertMsg(topic, sendMsg, 0)
end
